package com.hxkj.service.wechat.impl;

import com.hxkj.client.service.AppletServiceClient;
import com.hxkj.domain.enums.AppletExceptionEnum;
import com.hxkj.domain.enums.AppletTypeEnum;
import com.hxkj.domain.po.AppletConfigure;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.service.wechat.AppletService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 小程序列表service
 * <p>
 * Create by yaosy on 2018年6月11日20:33:11
 */
@Service("appletServiceImpl")
@Slf4j
public class AppletServiceImpl implements AppletService {

    @Autowired
    private AppletServiceClient appletServiceClient;

    @Autowired
    private TokenUtils tokenUtils;



    @Override
    public ResultPageStruct<AppletConfigure> getlist(AppletConfigure appletConfigure, PageTo pageParam) throws Exception{
        ParameterDataStruct<AppletConfigure> parameterDataStruct = new ParameterDataStruct<>();
        AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
        appletConfigure.setUserId(authUser.getUserId());
        parameterDataStruct.setData(appletConfigure);
        parameterDataStruct.setPageTo(pageParam);

        //将前端传过来的结束时间 + 一天减一秒
        if(appletConfigure.getExpirationTimeEnd()!=null){
            appletConfigure.setExpirationTimeEnd(appletConfigure.getExpirationTimeEnd() + 24 * 60 * 60 * 1000 - 1);
        }

        ResultPageStruct<AppletConfigure> resultPageStruct = new ResultPageStruct<>();
        try{
            resultPageStruct = appletServiceClient.getlist(parameterDataStruct);
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        resultPageStruct.setCode(null);
        resultPageStruct.setMessage(null);
        return resultPageStruct;
    }

    @Override
    public void save(AppletConfigure appletConfigure) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{

            throwException(appletConfigure);
            AuthUser auser = (AuthUser) tokenUtils.getTokenUserInfo();
            appletConfigure.setCreator(auser.getUserId());
            appletConfigure.setModifier(auser.getUserId());
            appletConfigure.setUpdateDate(System.currentTimeMillis());
            appletConfigure.setCreateTime(System.currentTimeMillis());
            resultMessageStruct = appletServiceClient.save(appletConfigure);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
            throw e;
        }
    }


    protected void throwException(AppletConfigure appletConfigure) throws Exception{
        if(StringUtils.isEmpty(appletConfigure.getAppletName())){
            throw new CustomException(AppletExceptionEnum.APPLET_NAME_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getQrCode())){
            throw new CustomException(AppletExceptionEnum.APPLET_QR_CODE_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getHeadUrl())){
            throw new CustomException(AppletExceptionEnum.APPLET_HEAD_URL_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getSmallRoutine())){
            throw new CustomException(AppletExceptionEnum.APPLET_SMALL_ROUTINE_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getAccount())){
            throw new CustomException(AppletExceptionEnum.APPLET_ACCOUNT_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getOriginalId())){
            throw new CustomException(AppletExceptionEnum.APPLET_ORIGINAL_NOT_BLANK);
        }
        if(appletConfigure.getExpirationTime()==null){
            throw new CustomException(AppletExceptionEnum.APPLET_EXPIRATION_TIME_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getAppId())){
            throw new CustomException(AppletExceptionEnum.APPLET_APP_ID_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getAppSecret())){
            throw new CustomException(AppletExceptionEnum.APPLET_APP_SECRET_NOT_BLANK);
        }
        if(appletConfigure.getAuditMode()==null){
            throw new CustomException(AppletExceptionEnum.APPLET_ADUIT_MODEA_NOT_BLANK);
        }
        if(appletConfigure.getSiteId()==null){
            throw new CustomException(AppletExceptionEnum.APPLET_SITE_ID_NOT_BLANK);
        }
        if(appletConfigure.getStatus()==null){
            throw new CustomException(AppletExceptionEnum.APPLET_STATUS_NOT_BLANK);
        }
        if(appletConfigure.getRelationArticlesNumber()==null){
            throw new CustomException(AppletExceptionEnum.APPLET_NUMBER_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getRequestLegalDomain())){
            throw new CustomException(AppletExceptionEnum.APPLET_REQUEST_LEGAL_DOMAIN_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getScoketLegalDomain())){
            throw new CustomException(AppletExceptionEnum.APPLET_SCOKET_LEGAL_DOMAIN_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getUploadLegalDomain())){
            throw new CustomException(AppletExceptionEnum.APPLET_UPLOAD_LEGAL_DOMAIN_NOT_BLANK);
        }
        if(StringUtils.isEmpty(appletConfigure.getDownloadLegalDomain())){
            throw new CustomException(AppletExceptionEnum.APPLET_DOWNLOAD_LEGAL_DOMAIN_NOT_BLANK);
        }
       /* if(StringUtils.isEmpty(appletConfigure.getBuinessLegalDomain())){
            throw new CustomException(AppletExceptionEnum.APPLET_BUINESS_LEGAL_DOMAIN_NOT_BLANK);
        }*/
        if (appletConfigure.getAuditMode().toString().equals(AppletTypeEnum.AUDUIT_MODE.getType())){//审核模式
            if (StringUtils.isEmpty(appletConfigure.getAppletNameConfigre())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_LEGAL_DOMAIN_NOT_BLANK);
            }
            if (StringUtils.isEmpty(appletConfigure.getHeadUrlConfigure())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_HEAD_URL_DOMAIN_NOT_BLANK);
            }
            if (StringUtils.isEmpty(appletConfigure.getBrief())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_BRIEF_DOMAIN_NOT_BLANK);
            }
            if (StringUtils.isEmpty(appletConfigure.getPhone())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_PHONE_NOT_BLANK);
            }
            if (StringUtils.isEmpty(appletConfigure.getParticulars())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_PARTICULARS_NOT_BLANK);
            }
            if (StringUtils.isEmpty(appletConfigure.getPhotoUrl())){
                throw new CustomException(AppletExceptionEnum.APPLET_CONFIGURE_PHOTO_URL_NOT_BLANK);
            }
        }
    }


    @Override
    public AppletConfigure get(AppletConfigure appletConfigure) {
        ResultDataStruct<AppletConfigure> resultDataStruct = new ResultDataStruct<>();
        try{
            if(appletConfigure.getAppletId()==null){
                throw new CustomException(AppletExceptionEnum.APPLET_ID_NOT_BLANK);
            }
            resultDataStruct = appletServiceClient.get(appletConfigure);
            if (resultDataStruct.getCode() != null) {
                throw new CustomException(AppletExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultDataStruct.getData();
    }

    public void modify(AppletConfigure appletConfigure) throws Exception{
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            throwException(appletConfigure);
            AuthUser authUser = (AuthUser) tokenUtils.getTokenUserInfo();
            appletConfigure.setUpdateDate(System.currentTimeMillis());
            appletConfigure.setModifier(authUser.getUserId());
            resultMessageStruct = appletServiceClient.modify(appletConfigure);
            if(resultMessageStruct.getCode()!=200){
                throw new CustomException(AppletExceptionEnum.getEnumByCode(resultMessageStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public AppletConfigure getModeList(AppletConfigure appletConfigure) {
        ResultDataStruct<AppletConfigure> resultDataStruct = new ResultDataStruct<>();
        try{
            if(appletConfigure.getAppletId()==null){
                throw new CustomException(AppletExceptionEnum.APPLET_ID_NOT_BLANK);
            }
            resultDataStruct = appletServiceClient.getModeList(appletConfigure);
            if (resultDataStruct.getCode() != null) {
                throw new CustomException(AppletExceptionEnum.getEnumByCode(resultDataStruct.getCode()));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return resultDataStruct.getData();
    }


}
